/**
 * 15/02/2014
 * 
 * CustomerDAO
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Customer;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of CustomerDAO
 * @author Javier Tejedor
 */
public class CustomerDAO implements CustomerDAOInterface {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Constructor
	 */
	public CustomerDAO () {

	}

	/**
	 * returns Hibernate session
	 * @return
	 */
	private Session getSession () {
		return sessionFactory.getCurrentSession();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#selectById(int)
	 */
	@Transactional(readOnly = true)
	public Customer selectById(int id) {
		Customer customer = (Customer) getSession().get(Customer.class, id);
		return customer;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#selectAll()
	 */
	@Transactional(readOnly = true)
	public List<Customer> selectAll() {
		List<Customer> customer = getSession().createQuery("from Customer").list();
		return customer;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#selectAllCount()
	 */
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<Customer> customer = getSession().createQuery("from Customer").list();
		return customer.size();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#insert(org.cuatrovientos.erp.model.Customer)
	 */
	@Transactional
	public void insert(Customer customer) {
		Integer id = (Integer) getSession().save(customer);
		customer.setId(id);           
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#update(org.cuatrovientos.erp.model.Customer)
	 */
	@Transactional
	public void update(Customer customer) {
		getSession().merge(customer);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#delete(org.cuatrovientos.erp.model.Customer)
	 */
	@Transactional
	public void delete(Customer customer) {
		getSession().delete(customer);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#saveOrUpdate(org.cuatrovientos.erp.model.Customer)
	 */
	@Transactional
	public void saveOrUpdate(Customer customer) {
		getSession().saveOrUpdate(customer);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#deleteById(int)
	 */
	@Transactional
	public void deleteById(int id) {
		Customer customer = (Customer) getSession().load(Customer.class,id);
		getSession().delete(customer);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.CustomerDAOInterface#search(java.lang.String)
	 */
	@Transactional(readOnly = true)
	public List<Customer> search(String name) {
		Query query = 
				getSession().
				createQuery("from Customer s where s.name like :name");
		
		query.setParameter("name", "%" + name + "%");

		List<Customer> customer = query.list();

		return customer;
	}

}
